﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace QuantitativeIndicator.Hurst
{
    class OutputSQL
    {
        private HurstDataModel db;
        private HurstCaculatorRS tmpHurst;//Hurst计算模块
        
        public OutputSQL()
        {
            this.db = new HurstDataModel();
             this.tmpHurst = new HurstCaculatorRS();
        }
        //输入R/S值以及V统计量，V=(R/S)/sqrt(N)
        public void OutputRS(int noOfCalPoints, string indexTradingCode, string calDate)
        {
            //插入要满足几个条件：
            //1. 能有足够的点计算RS值
            //2. 与表中没有重复数据
            int leftPoints = db.getIndexTradedDatesList(indexTradingCode,"10000000",calDate).Count();
            if ((noOfCalPoints + 1) < leftPoints)
            {
                if (db.isExitInDFZQ_RSofHurst(calDate, indexTradingCode, noOfCalPoints) == false)
                {
                    double[,] tmpRS = tmpHurst.CaculateRS(noOfCalPoints, indexTradingCode, calDate);
                    double[] VStat = tmpHurst.CaculateVStat(tmpRS);
                    for (int i = 0; i < tmpRS.GetLength(1); i++)
                    {
                        db.insertDFZQ_RSofHurst(calDate, indexTradingCode, noOfCalPoints, tmpRS[0, i], tmpRS[1, i], VStat[i]);
                    }
                }
            }
        }
        //输出Hurst指数,calPoints,code,date,hurstStyle是主键
        public void OutputHurst(int noOfCalPoints, string indexTradingCode, string startDate,string endDate,string hurstStyle)
        {
            double tmpHurstExp;
            List<string> dateList1 = new List<string>();
            List<string> dateList2=new List<string>();

            dateList1 = db.getTradedDateListFromDFZQ_HurstExponent(indexTradingCode,noOfCalPoints, hurstStyle);//得到已经在表内计算过的交易日
            dateList2 = db.getIndexTradedDatesList(indexTradingCode,startDate,endDate);//需要插入的交易日，即现实中存在的交易日
           
            //插入DFZQ_HurstExponent,这里有3个条件才进行插入动作
            //1. 需要插入的日期在与已经计算过的日期不重复
            //2. 需要插入的日期没有超过现实中最近一个交易日
            //3. 当前这个日期之前是否保证有足够的点来计算Hurst指数
            foreach (string element in dateList2)
            {
                Console.WriteLine(element);
                List<string> tmpDateList = new List<string>();
                tmpDateList = db.getIndexTradedDatesList(indexTradingCode, "10000000", element);
                if ((noOfCalPoints+1) <= tmpDateList.Count())
                {
                    if (dateList1.Contains(element) == false)
                    {
                        if (db.isOutOfIndexLastTradedDate(indexTradingCode, element) == false)
                        {
                            tmpHurstExp = tmpHurst.CaculateHurst(noOfCalPoints, indexTradingCode, element);
                            db.insertDFZQ_HurstExponent(element, indexTradingCode, noOfCalPoints, hurstStyle, tmpHurstExp);
                        }
                    }
                }
            }      
        }
    }
}
